<template>
  <div id="app">
    <router-view />

    <div class="loading-mask" v-show="loading">
      <div class="pc-loading" v-if="isPC">
        <i class="icon el-icon-loading"></i>
        <p class="text">加载中...</p>
      </div>
      <div class="m-loading" v-else></div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations } from 'vuex'
const mobileWidth = 640
const padWidth = 1024

export default {
  computed: {
    ...mapState({
      loading: state => state.loading
    }),
    ...mapGetters({
      isPC: 'isPC'
    })
  },
  mounted () {
    const device = this.checkDevice()
    this.setDevice(device)
  },
  beforeMount () {
    window.addEventListener('resize', this.resizeHandler)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.resizeHandler)
  },
  methods: {
    ...mapMutations({
      setDevice: 'setDevice'
    }),
    checkDevice () {
      const width = document.body.clientWidth
      if (width <= mobileWidth) {
        return 'mobile'
      }
      if (width > padWidth) {
        return 'pc'
      }
      return 'pad'
    },
    resizeHandler () {
      if (!document.hidden) {
        let device = this.checkDevice()
        this.setDevice(device)
      }
    }
  }
}
</script>

<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #333333;
}

.loading-mask {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #409EFF;
  /*background: rgba(255, 255, 255, 0.7);*/
  .pc-loading {
    text-align: center;
    .icon {
      font-size: 26px;
    }
    .text {
      margin-top: 10px;
      font-size: 12px;
    }
  }
  .m-loading {
    display: block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    position: relative;
    animation: loaderM 1s ease-in-out infinite;
    &::before {
      content: "";
      position: absolute;
      top: 0;
      left: -16px;
      width: 10px;
      height: 10px;
      border-radius: 50%;
      animation: loaderL 1s ease-in-out infinite;
    }
    &::after {
      content: "";
      position: absolute;
      top: 0;
      right: -16px;
      width: 10px;
      height: 10px;
      border-radius: 50%;
      animation: loaderR 1s ease-in-out infinite;
    }
    @keyframes loaderL {
      0% { background-color: #c6e2ff; }
      25% { background-color: #409eff; }
      50% { background-color: #c6e2ff; }
      75% { background-color: #c6e2ff; }
      100% { background-color: #c6e2ff; }
    }
    @keyframes loaderM {
      0% { background-color: #c6e2ff; }
      25% { background-color: #c6e2ff; }
      50% { background-color: #409eff; }
      75% { background-color: #c6e2ff; }
      100% { background-color: #c6e2ff; }
    }
    @keyframes loaderR {
      0% { background-color: #c6e2ff; }
      25% { background-color: #c6e2ff; }
      50% { background-color: #c6e2ff; }
      75% { background-color: #409eff; }
      100% { background-color: #c6e2ff; }
    }
  }
}
</style>
